package common

import (
	"context"
	"errors"
	"fmt"
	"strconv"
)

func GetUserIDFromCtx(ctx context.Context) (int64, error) {
	userVal := ctx.Value("user_id")
	if userVal == nil {
		return 0, errors.New("user_id is nil")
	}

	userIdStr := userVal.(string)
	if userIdStr == "" {
		return 0, fmt.Errorf("user_id is not string")
	}

	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil {
		return 0, errors.New("user_id is not string")
	}

	if userId == 0 {
		return 0, errors.New("user_id is not int64")
	}
	return userId, nil
}
